package com.baidu.location.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.entity.FriendRecord;
import com.entity.Pagination;
import com.entity.Response;
import com.listview.FriendrequestAdapter;
import com.listview.FriendrequestList;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.util.DialogUtil;
import com.util.HttpUtils;

import java.util.ArrayList;
import java.util.List;

public class FriendRequestActivity extends AppCompatActivity {
    private ListView friendRequestList;
    private final List<FriendrequestList> friendrequestList=new ArrayList<>();
    private FriendrequestAdapter friendrequestAdapter;
    public FriendRequestActivity(){
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_request);
        friendRequestList=findViewById(R.id.friendRequestList);
        new Thread(){
            @Override
            public void run() {
                super.run();
                Response<Pagination<List<FriendRecord>>> friend= JSONObject.parseObject(HttpUtils.Get("Friends/GetFriendRecordsList"),new TypeReference<Response<Pagination<List<FriendRecord>>>>(){});
                if(friend.getStatusCode()==200){
                    List<FriendRecord> data= friend.getValue().getData();
                    for (FriendRecord f:data) {
                        FriendrequestList fs=new FriendrequestList();
                        fs.setId(f.getId());
                        fs.setImage(f.getRespondent().getHeadPortrait());
                        fs.setName(f.getRespondent().getName());
                        fs.setRemark(f.getRemark());
                        fs.setFriendStatus(f.getFriendStatus());
                        friendrequestList.add(fs);
                    }
                    FriendRequestActivity.this.runOnUiThread(() -> {
                        friendrequestAdapter=new FriendrequestAdapter(FriendRequestActivity.this,R.layout.friendrequestlistlayout,friendrequestList,FriendRequestActivity.this);
                        friendRequestList.setAdapter(friendrequestAdapter);
                    });
                }else {
                    DialogUtil.CreateDoalog(FriendRequestActivity.this,friend.getMessage(),2000, QMUITipDialog.Builder.ICON_TYPE_FAIL);
                }
            }
        }.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }
}